package com.lmpop.trade.ts_category;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.util.RandomID;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Ts_categoryInfo
  implements TradeInterf
{
  QueryList queryList;
  RandomID randId;

  public Ts_categoryInfo()
  {
    this.queryList = new QueryList();
    this.randId = new RandomID();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ts_category ts_category = new Ts_category();

    ts_category.setUp_cat_id(map.getString("up_cat_id"));
    ts_category.setCat_level(map.getString("cat_level"));
    ts_category.setClass_type(map.getString("class_type"));
    ts_category.setIs_dis(map.getString("is_dis"));
    ts_category.setSort_no(map.getInt("sort_no"));
    ts_category.setPyKey(map.getString("pyKey"));
    ts_category.setRemark(map.getString("remark"));

    String result_code = "0";
    try
    {
      String cat_name = map.getString("cat_name");
      String[] cat_name_group = cat_name.split(" ");
      if ((!cat_name.equals("")) && (cat_name_group.length > 0))
        for (int i = 0; i < cat_name_group.length; i++)
          if (!cat_name_group[i].equals("")) {
            ts_category.setCat_id(this.randId.GenTradeId());
            ts_category.setCat_name(cat_name_group[i]);
            insert(dbmMgr, ts_category);
          }
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ts_category ts_category)
    throws ApplicationException
  {
    dbmMgr.update("insertTs_category", ts_category);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");

    String result_code = "0";
    String cat_ids = "";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        StringBuffer pcat = new StringBuffer();
        for (int i = 0; i < pkStr.length; i++)
        {
          if (pkStr[i].equals(""))
            continue;
          pcat.append(pkStr[i] + "|");
          pcat.append(getAllCatAttr(pkStr[i]));
        }

        cat_ids = pcat.toString();
        String[] catIds = pcat.toString().split("\\|");
        for (String id : catIds)
        {
          delete(dbmMgr, id);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }

    map.setString("cat_ids", cat_ids);
    map.setString("result_code", result_code);
    return map;
  }

  public String getAllCatAttr(String pk)
  {
    StringBuffer catAttr = new StringBuffer();
    List sonlist = getSonById(pk);
    if ((sonlist != null) && (sonlist.size() > 0))
    {
      for (int i = 0; i < sonlist.size(); i++)
      {
        Hashtable sonMap = (Hashtable)sonlist.get(i);
        String cat_id = "";
        if (sonMap.get("cat_id") != null)
        {
          cat_id = sonMap.get("cat_id").toString();
        }
        if (cat_id.equals(""))
          continue;
        catAttr.append(cat_id + "|");
        if (!checkSonById(cat_id).booleanValue())
          continue;
        catAttr.append(getAllCatAttr(cat_id));
      }

    }

    return catAttr.toString();
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
	  //delete from ts_category where cat_id=#value#
    dbmMgr.update("deleteTs_category", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ts_category ts_category = new Ts_category();
    ts_category.setCat_id(map.getString("cat_id"));
    ts_category.setCat_name(map.getString("cat_name"));
    ts_category.setUp_cat_id(map.getString("up_cat_id"));
    ts_category.setCat_level(map.getString("cat_level"));
    ts_category.setClass_type(map.getString("class_type"));
    ts_category.setIs_dis(map.getString("is_dis"));
    ts_category.setSort_no(map.getInt("sort_no"));
    ts_category.setPyKey(map.getString("pyKey"));
    ts_category.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      update(dbmMgr, ts_category);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ts_category ts_category)
    throws ApplicationException
  {
    dbmMgr.update("updateTs_category", ts_category);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Map ts_category = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ts_category.put("start", Integer.valueOf(start));
    ts_category.put("limit", Integer.valueOf(limit));

    return this.queryList.getList("selTs_categoryList", ts_category);
  }

  public int getCountByObj(Object obj)
  {
    Map ts_category = (Hashtable)obj;

    List list = this.queryList.getList("selTs_categorySum", ts_category);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTs_categoryByPk", pk);
  }

  public String getCatNameById(String pk)
  {
    List catList = this.queryList.getList("getTs_categoryNameByPk", pk);
    String cat_name = "";
    if ((catList != null) && (catList.size() > 0)) {
      Hashtable catMap = (Hashtable)catList.get(0);
      if (catMap.get("cat_name") != null) cat_name = catMap.get("cat_name").toString();
    }
    return cat_name;
  }

  public Boolean checkSonById(String pk)
  {
    List catList = this.queryList.getList("checkTs_categorySonByPk", pk);

    if ((catList != null) && (catList.size() > 0)) {
      return Boolean.valueOf(true);
    }
    return Boolean.valueOf(false);
  }

  public int checkCatSonById(String pk)
  {
    List catList = this.queryList.getList("checkTs_categorySonByPk", pk);
    if ((catList != null) && (catList.size() > 0))
    {
      return 0;
    }
    return 1;
  }

  public List getSonById(String pk)
  {
    return this.queryList.getList("checkTs_categorySonByPk", pk);
  }

  public SysMap categorySort(DbmInterf dbmMgr, SysMap map)
  {
    Ts_category ts_category = new Ts_category();
    String pkid_string = map.getString("sort");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
        {
          if (pkStr[i].equals(""))
            continue;
          String[] sort = pkStr[i].split(",");
          if (sort.length == 1)
          {
            ts_category.setCat_id(sort[0]);
            ts_category.setSort_no(0);
            updateSort(dbmMgr, ts_category);
          }
          if (sort.length != 2)
            continue;
          ts_category.setCat_id(sort[0]);
          ts_category.setSort_no(Integer.parseInt(sort[1]));
          updateSort(dbmMgr, ts_category);
        }
      }
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateSort(DbmInterf dbmMgr, Ts_category ts_category) throws ApplicationException {
    dbmMgr.update("updateTs_categorySort", ts_category);
  }

  public String getSelCatByTLevel(String type, String level)
  {
    Ts_category ts_category = new Ts_category();
    String select = "";
    ts_category.setClass_type(type);
    ts_category.setCat_level(level);
    List list = this.queryList.getList("checkTs_categoryByTypeLevel", ts_category);
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        String id = ""; String text = "";
        if (map.get("cat_id") != null) {
          id = map.get("cat_id").toString();
        }
        if (map.get("cat_name") != null) {
          text = map.get("cat_name").toString();
        }
        select = select + "<option value=" + id + ">" + text + "</option>";
      }
    }
    return select;
  }

  public Map getCatClassByUpId(String upclass, String type)
  {
    Ts_category ts_category = new Ts_category();
    ts_category.setUp_cat_id(upclass);
    ts_category.setClass_type(type);
    Map classMap = new Hashtable();
    String class_id = ""; String class_name = "";
    List list = this.queryList.getList("checkTs_categoryByTypeUpCatId", ts_category);
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        if (map.get("cat_name") != null) {
          class_name = map.get("cat_name").toString();
        }
        if (map.get("cat_id") != null) {
          class_id = map.get("cat_id").toString();
        }

        classMap.put(class_id, class_name);
      }
    }

    return classMap;
  }

  public Map getCatClassMap(String type)
  {
    Map classMap = new Hashtable();
    String class_id = ""; String class_name = "";
    List list = this.queryList.getList("getAllTs_categoryByType", type);
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        if (map.get("cat_name") != null) {
          class_name = map.get("cat_name").toString();
        }
        if (map.get("cat_id") != null) {
          class_id = map.get("cat_id").toString();
        }

        classMap.put(class_id, class_name);
      }
    }

    return classMap;
  }

  public String getUp_cat_id(String cat_id)
  {
    String up_cat_id = "";
    List Idlist = this.queryList.getList("getUp_cat_id", cat_id);
    if ((Idlist != null) && (Idlist.size() > 0)) {
      Map chmap = (Hashtable)Idlist.get(0);
      if (chmap.get("up_cat_id") != null)
      {
        up_cat_id = chmap.get("up_cat_id").toString();
      }
    }
    return up_cat_id;
  }

  public String getCat_nameById(String cat_id)
  {
    String cat_name = "";
    List Idlist = this.queryList.getList("getCat_nameById", cat_id);
    if ((Idlist != null) && (Idlist.size() > 0)) {
      Map chmap = (Hashtable)Idlist.get(0);
      if (chmap.get("cat_name") != null)
      {
        cat_name = chmap.get("cat_name").toString();
      }
    }
    return cat_name;
  }

  public String getCategoryTree(String cat_id, String class_type, String team_type)
  {
    StringBuffer select = new StringBuffer();
    String cat_idx = ""; String cat_namex = ""; String cat_level = "";
    Hashtable catMap = new Hashtable();
    catMap.put("cat_id", cat_id);
    catMap.put("class_type", class_type);
    List list = this.queryList.getList("getCategoryTreeByCLevel", catMap);
    if ((list != null) && (list.size() > 0))
    {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        if (map.get("cat_id") != null) cat_idx = map.get("cat_id").toString();
        if (map.get("cat_name") != null) cat_namex = map.get("cat_name").toString();
        if (map.get("cat_level") != null) cat_level = map.get("cat_level").toString();
        StringBuffer trr = new StringBuffer();
        String ch_tr = cat_idx + "|" + getTreeUpId(cat_idx);
        if (!ch_tr.equals("")) {
          String[] tr = ch_tr.split("\\|");
          for (int j = tr.length - 1; j >= 0; j--)
          {
            trr.append(tr[j] + "|");
          }
        }
        String line = "";
        for (int j = 1; j < Integer.parseInt(cat_level); j++)
        {
          line = line + "&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        if (checkCatSonById(cat_idx) == 1) {
          if (!team_type.equals(trr.toString()))
            select.append("<option value=" + trr.toString() + ">" + line + cat_namex + "</option>");
          else
            select.append("<option value=" + trr.toString() + " selected>" + line + cat_namex + "</option>");
        } else {
          if (!team_type.equals(trr.toString()))
            select.append("<option value=" + trr.toString() + ">" + line + cat_namex + "</option>");
          else
            select.append("<option value=" + trr.toString() + " selected>" + line + cat_namex + "</option>");
          select.append(getCategoryTree(cat_idx, class_type, team_type));
        }
      }
    }

    return select.toString();
  }

  public String getTreeUpId(String cat_id)
  {
    String up_id = "";
    StringBuffer up_ids = new StringBuffer();
    List Idlist = this.queryList.getList("getUp_cat_id", cat_id);
    if ((Idlist == null) || (Idlist.size() <= 0)) return "";
    for (Iterator it = Idlist.iterator(); it.hasNext(); ) {
      Hashtable map = (Hashtable)it.next();
      if (map.get("up_cat_id") != null)
      {
        up_id = map.get("up_cat_id").toString();
        if (up_id.equals("000000000000000")) break;
        up_ids.append(up_id + "|");
        up_ids.append(getTreeUpId(up_id));
      }
    }

    return up_ids.toString();
  }

  public String getCheckedCategoryTree(String cat_id, String class_type, String param_cat_id)
  {
    StringBuffer select = new StringBuffer();
    String cat_idx = ""; String cat_namex = ""; String cat_level = "";
    Hashtable catMap = new Hashtable();
    catMap.put("cat_id", cat_id);
    catMap.put("class_type", class_type);
    List list = this.queryList.getList("getCategoryTreeByCLevel", catMap);

    if ((list != null) && (list.size() > 0))
    {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        if (map.get("cat_id") != null) cat_idx = map.get("cat_id").toString();
        if (map.get("cat_name") != null) cat_namex = map.get("cat_name").toString();
        if (map.get("cat_level") != null) cat_level = map.get("cat_level").toString();
        StringBuffer trr = new StringBuffer();
        String ch_tr = cat_idx + "|" + getTreeUpId(cat_idx);
        if (!ch_tr.equals("")) {
          String[] tr = ch_tr.split("\\|");
          for (int j = tr.length - 1; j >= 0; j--)
          {
            trr.append(tr[j] + "|");
          }
        }
        String line = "";
        for (int j = 1; j < Integer.parseInt(cat_level); j++)
        {
          line = line + "&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        String check = "";
        if (trr.toString().indexOf(param_cat_id) > -1) {
          check = " selected ";
        }
        if (checkCatSonById(cat_idx) == 1) {
          select.append("<option value='" + trr.toString() + "'" + check + ">" + line + cat_namex + "</option>");
        } else {
          select.append("<option value='" + trr.toString() + "'" + check + ">" + line + cat_namex + "</option>");
          select.append(getCategoryTree(cat_idx, class_type, param_cat_id));
        }
      }
    }

    return select.toString();
  }

  public Map getAllChClass()
  {
    Map catMap = new Hashtable();
    Map classMap = new Hashtable();
    String class_id = ""; String class_name = "";
    List list = this.queryList.getList("getAllCatClassByNothing", catMap);
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        if (map.get("cat_name") != null) {
          class_name = map.get("cat_name").toString();
        }
        if (map.get("cat_id") != null) {
          class_id = map.get("cat_id").toString();
        }

        classMap.put(class_id, class_name);
      }
    }

    return classMap;
  }

  public List getGoodsAllCategory()
  {
    StringBuffer buf = new StringBuffer();
    List list = this.queryList.getList("getOneGoodsCats");
    return list;
  }
  public String getOneGoodsCats(String user_id, String area_attr, String area_name) {
    StringBuffer buf = new StringBuffer();
    List list = this.queryList.getList("getOneGoodsCats");
    String cat_name = ""; String cat_id = "";
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        if (map.get("cat_name") != null) {
          cat_name = map.get("cat_name").toString();
        }
        if (map.get("cat_id") != null) {
          cat_id = map.get("cat_id").toString();
        }
        if ((area_name != null) && (!"".equals(area_name)))
          try {
            buf.append("<A href='/mallcat_list.html?user_id=" + user_id + "&area_name=" + URLEncoder.encode(area_name, "UTF-8") + "&area_attr=" + area_attr + "&cat_attr=" + cat_id + "'>" + cat_name + "</A>");
          } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
          }
        else {
          buf.append("<A href='/mallcat_list.html?user_id=" + user_id + "&area_attr=" + area_attr + "&cat_attr=" + cat_id + "'>" + cat_name + "</A>");
        }
      }
    }

    return buf.toString();
  }
}